package cn.hk.user.controller;

import cn.hk.common.property.WeixinProperties;
import cn.hk.user.domain.UserInfo;
import cn.hk.user.domain.vo.*;
import cn.hk.user.service.UserInfoService;
import cn.hk.user.service.UserRelationService;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.KelinUserInfoBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api("用户服务")
@RestController
@RequestMapping("/userservice/user")
@Anonymous
public class
UserController extends BaseController {
    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private WeixinProperties weixinProperties;

    @Autowired
    private UserRelationService userRelationService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 测试
     * @param
     * @return
     */
    @GetMapping("/test")
    public String test() throws Exception {

        return  "测试";
    }


    /**
     * 微信登录,并获取sessionKe和openId
     * @param wxLoginBodyVo
     * @return
     */
    @PostMapping("/wxLogin")
    @ApiOperation("微信登录")
    @RepeatSubmit
    public AjaxResult wxLogin(@Validated @RequestBody WxLoginBodyVo wxLoginBodyVo) throws Exception {
        Map<String,Object> resulMap = userInfoService.wxLogin(wxLoginBodyVo);
       return AjaxResult.success().put("item", resulMap);
    }

    /**
     * 手机号登录，并获取token
     * @param wxLoginUserInfoVo
     * @return
     * @throws Exception
     */
    @ApiOperation("手机号登录")
    @PostMapping("/wxLoginByPhone")
    @RepeatSubmit
    public AjaxResult wxLoginByPhone(@Validated @RequestBody WxLoginUserInfoVo wxLoginUserInfoVo) throws Exception {
        return userInfoService.wxLoginByPhone(wxLoginUserInfoVo);
    }

    /**
     * 完善登录用户信息
     * @param wxLoginUserInfo
     * @return
     * @throws Exception
     */
    @ApiOperation("完善登录用户信息")
    @PostMapping("/addLoginUserInfo")
    @RepeatSubmit
    public AjaxResult addLoginUserInfo( @RequestBody UserInfo wxLoginUserInfo) throws Exception {
        return userInfoService.addWxLoginUserInfo(wxLoginUserInfo);
    }

    /**
     * 根据userId登录
     * @return
     */
    @ApiOperation("切换账号")
    @GetMapping("/wxLoginByUserId/{userId}")
    @RepeatSubmit
    public AjaxResult wxLoginByUserId(@PathVariable("userId")Long UserId){
        String token = userInfoService.wxLoginByUserId(UserId);
       return AjaxResult.success().put("token",token);
    }


    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @ApiOperation("获取kelin用户信息")
    @GetMapping("getWxInfo")
    public AjaxResult getInfo(){
        KelinUserInfoBody user = SecurityUtils.getLoginUser().getUser().getKelinUserInfoBody();
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtil.copyProperties(user, userInfoVo);
        return AjaxResult.success().put("item",userInfoVo);
    }


    /**
     * 用户信息修改
     * @param userInfo
     * @return
     */
    @PostMapping("/update")
    @RepeatSubmit
    @ApiOperation("用户信息修改")
    public AjaxResult update(@ApiParam("用户信息")@RequestBody UserInfo userInfo){
        return userInfoService.editById(userInfo);
    }

    /**
     *添加家庭成员
     * @param familyPersonVo
     * @return
     */
    @PostMapping("/addFamilyMember")
    @RepeatSubmit
    @ApiOperation("添加家庭成员")
    public AjaxResult addFamilyMember(@ApiParam("家庭成员信息")@RequestBody FamilyPersonVo familyPersonVo){

        return  userInfoService.addFamilyMember(familyPersonVo);
    }
    /**
     *添加家庭成员信息
     * @param familyPersonVo
     * @return
     */
    @PostMapping("/addFamilyMemberInfo")
    @RepeatSubmit
    @ApiOperation("添加家庭成员")
    public AjaxResult addFamilyMemberInfo(@ApiParam("家庭成员信息")@RequestBody FamilyPersonVo familyPersonVo){

        return  userInfoService.addFamilyMemberInfo(familyPersonVo);
    }

    /**
     * 家庭成员列表
     * @param userId
     * @return
     */
    @ApiOperation("家庭成员列表")
    @GetMapping("/familyMemberList/{userId}")
    public AjaxResult familyMemberList(@ApiParam("用户id")@PathVariable Long userId){
        List<FamilyDetailVo> familyListDetailListVo = userRelationService.familyMemberList(userId);
        return AjaxResult.success().put("item", familyListDetailListVo);
    }

    /**
     * 移除家庭成员
     * @param
     * @return
     */
    @PostMapping("/removeFamilyMember")
    @ApiOperation("移除家庭成员")
    public AjaxResult removeFamilyMember(@RequestBody ChangeVo changeVo){

        return userRelationService.removeFamilyMember(changeVo);
    }

    /**
     * 修改家庭成员关系
     * @param changeVo
     * @return
     */
    @PostMapping("/editFamilyMemberRelation")
    @ApiOperation("修改家庭成员关系")
    public AjaxResult editFamilyRelation(@RequestBody ChangeVo changeVo){
        return userRelationService.editFamilyMemberRelation(changeVo);
    }


}
